package com.zhj.agent.interceptor.resttemplate.sync;

import com.zhj.agent.support.AbstractClassEnhancePluginDefine;
import com.zhj.agent.support.InstanceMethodsInterceptPoint;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

import static net.bytebuddy.matcher.ElementMatchers.named;

/**
 * <p>Company: 成都返空汇网络技术有限公司</p>
 * <p>Description:  </p>
 *
 * @author zhonghaijun
 * @version 1.0.0
 * @email "mailto:zhonghaijun@fkhwl.com"
 * @date 2021.12.02 14:22
 * @since 1.0.0
 */
public class RestTemplateInstrumentation extends AbstractClassEnhancePluginDefine {

    /** ENHANCE_CLASS */
    private static final String ENHANCE_CLASS = "com.fkhwl.agent.adapter.client.AgentTemplate";

    /** DO_EXECUTE_METHOD_NAME */
    private static final String DO_EXECUTE_METHOD_NAME = "doExecute";

    /** DO_EXECUTE_INTERCEPTOR */
    private static final Class<?> DO_EXECUTE_INTERCEPTOR = RestExecuteInterceptor.class;

    /** HANDLE_REQUEST_METHOD_NAME */
    private static final String HANDLE_REQUEST_METHOD_NAME = "handleResponse";

    /** HAND_REQUEST_INTERCEPTOR */
    private static final Class<?> HAND_REQUEST_INTERCEPTOR = RestResponseInterceptor.class;

    /** CREATE_REQUEST_METHOD_NAME */
    private static final String CREATE_REQUEST_METHOD_NAME = "createRequest";

    /** CREATE_REQUEST_INTERCEPTOR */
    private static final Class<?> CREATE_REQUEST_INTERCEPTOR = RestRequestInterceptor.class;

    public RestTemplateInstrumentation() {
        super(ENHANCE_CLASS);
    }

    /**
     * Enhance instance
     *
     * @return the instance methods intercept point [ ]
     * @since 1.0.0
     */
    @Override
    public InstanceMethodsInterceptPoint[] enhanceInstance() {
        return new InstanceMethodsInterceptPoint[] {
            new InstanceMethodsInterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named(CREATE_REQUEST_METHOD_NAME);
                }

                @Override
                public Class<?> getMethodsInterceptor() {
                    return CREATE_REQUEST_INTERCEPTOR;
                }

                @Override
                public boolean isOverrideArgs() {
                    return false;
                }
            },
            new InstanceMethodsInterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named(DO_EXECUTE_METHOD_NAME);
                }

                @Override
                public Class<?> getMethodsInterceptor() {
                    return DO_EXECUTE_INTERCEPTOR;
                }

                @Override
                public boolean isOverrideArgs() {
                    return false;
                }
            },
            new InstanceMethodsInterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named(HANDLE_REQUEST_METHOD_NAME);
                }

                @Override
                public Class<?> getMethodsInterceptor() {
                    return HAND_REQUEST_INTERCEPTOR;
                }

                @Override
                public boolean isOverrideArgs() {
                    return false;
                }
            }
        };
    }
}
